<?php

//平台优惠券函数库
// +----------------------------------------------------------------------
// | TAODUDU.COM  [ Simple Efficient Excellent ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016 http://www.taodudu.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: ubten <462193409@qq.com> http://www.ubten.com/
// +----------------------------------------------------------------------
// 

function shijipay($total,$order_id,$pay_sn){
    return price($total - Snapup($order_id,$pay_sn));
}

//显示商品优惠的信息
function Snapup($order_id,$pay_sn){
    $b=0;
    //查询订单的总价
    $money = M('order')->where('order_id='.$order_id)->getField('goods_amount');
    $where['pay_sn'] = $pay_sn;
    $where['voucher_store'] = 0;
    $a = M('order_voucher')->where($where)->select();
    foreach ($a as $key=> $value) {
        
        if($value['voucher_type'] == 3){
            $limit[$key] = M('p_mansong_rule')->where('rule_id='.$value['voucher_code'])->getField('price');
        }else{
            $limit[$key] = M('p_voucher')->where('voucher_id='.$value['voucher_code'])->getField('voucher_limit');
        }
        //p($value['voucher_code']);
        if($money >= $limit[$key]){
            $b  += $value['voucher_price'];
        }else{
            $b  +=0;
        }
    }
    $b = price($b);
    return $b; 
}





function SnapupList($pay_sn){
    $where['pay_sn'] = $pay_sn;
    $where['voucher_store'] = 0;
    $a = M('order_voucher')->where($where)->select();
    foreach ($a as $value) {
        $b  .= $value['voucher_desc'];
    }
    return $b; 
}

//获取用户的代金券
//
function getUserVoucherCount($voucher_id,$store_id){
    $user_id = session('user_id');
    if(!empty($user_id)){
        $where['voucher_t_id'] = $voucher_id;
        $where['voucher_owner_id']  = session('user_id');
        $where['voucher_state'] = 1;
        $userCount= M('p_voucher')->where($where)->count();
        if($userCount > 0){
            $html = '<div class="q-state"><div class="btn-state btn-geted">已领取</div></div>';
            $html .= '<div class="q-opbtns ">';
            $html .= '<a href="/Supplier/'.$store_id.'" target="_blank" class="get-coupon q-btn get-coupon" clstag="false" title="免费抢券">';
            $html .= '<b class="semi-circle"></b><span>立即使用</span></a></div>';
        }else{
            $html = '<div class="q-opbtns ">';
            $html .= '<a href="javascript:void(0);" class="get-coupon q-btn get-coupon" clstag="true" title="免费抢券">';
            $html .= '<b class="semi-circle"></b><span>立即领取</span></a></div>';
        }
    }else{
            $html = '<div class="q-opbtns ">';
            $html .= '<a href="javascript:void(0);" class="get-coupon q-btn get-coupon" clstag="true" title="免费抢券">';
            $html .= '<b class="semi-circle"></b><span>立即领取</span></a></div>';
    }
    return $html;
}



//发放代金券
//voucher_id 代金券的ID
//user_tel   用户的手机号
function sendVoucher($voucher_id,$user_id){
    $map['voucher_id'] = $voucher_id;
    $voucher = M('p_voucher_rule')->where($map)->find();
    $where['user_id'] = $user_id;
    $user = M('users')->where($where)->find();
    $openID = M('weixin_user')->where($where)->getField('openid');
    if(!empty($voucher) && !empty($user)){
        $data['voucher_t_id'] = $voucher['voucher_id'];
        $data['voucher_title'] = $voucher['voucher_title'];
        $data['voucher_desc'] = $voucher['voucher_desc'];
        $data['voucher_start_date'] = $voucher['voucher_start_date'];
        $data['voucher_end_date'] = $voucher['voucher_end_date'];
        $data['voucher_price'] = $voucher['voucher_price'];
        $data['voucher_limit'] = $voucher['voucher_limit'];
        $data['voucher_store_id'] = $voucher['voucher_store_id'];
        $data['voucher_storename'] = $voucher['voucher_storename'];
        $data['voucher_state'] = $voucher['voucher_state'];
        $data['voucher_active_date'] = NOW_TIME;
        $data['voucher_type'] = 1;
        $data['voucher_owner_id'] = $user_id;
        $data['voucher_owner_name'] = $user['user_name'];
        $re = M('p_voucher')->add($data);
        //发送消息
        $kefu = new \Libs\WeiXin\CustomService();
        $kefu->sendText($openID,'恭喜您获取的1张 【'.$voucher['voucher_title'].'】, 详细信息可到【个人中心】-【我的红包】中查看下！');
    }else{
        $re = 0;
    }
    return $re;
}


//中奖发放代金券
//voucher_id 代金券的ID
//user_tel   用户的手机号
function sendPrizeVoucher($voucher_id,$user_id,$user_name){
    $map['voucher_id'] = $voucher_id;
    $voucher = M('p_voucher_rule')->where($map)->find();
    $where['user_id'] = $user_id;
    $openID = M('weixin_user')->where($where)->getField('openid');
    if(!empty($voucher)){
        $data['voucher_t_id'] = $voucher['voucher_id'];
        $data['voucher_title'] = $voucher['voucher_title'];
        $data['voucher_desc'] = $voucher['voucher_desc'];
        $data['voucher_start_date'] = $voucher['voucher_start_date'];
        $data['voucher_end_date'] = $voucher['voucher_end_date'];
        $data['voucher_price'] = $voucher['voucher_price'];
        $data['voucher_limit'] = $voucher['voucher_limit'];
        $data['voucher_store_id'] = $voucher['voucher_store_id'];
        $data['voucher_storename'] = $voucher['voucher_storename'];
        $data['voucher_state'] = $voucher['voucher_state'];
        $data['voucher_active_date'] = NOW_TIME;
        $data['voucher_type'] = 1;
        $data['voucher_owner_id'] = $user_id;
        $data['voucher_owner_name'] = $user_name;
        $re = M('p_voucher')->add($data);

        //减去优惠券相应的数量相应的数量
        $maps['voucher_id'] = $voucher['voucher_id'];
        M('p_voucher_rule')->where($maps)->setInc('voucher_giveout', 1);
        
        //发送消息
        $kefu = new \Libs\WeiXin\CustomService();
        $kefu->sendText($openID,'恭喜您获抽中1张 【'.$voucher['voucher_title'].'】, 详细信息可到【个人中心】-【我的红包】中查看下！');
    }else{
        $re = 0;
    }
    return $re;
}

// 
//获取商品运费的值
function getTransport($transport_id, $goodsPrice, $goodsNum) {
    if ($transport_id != 0) {
        //获取商品的运费 
        $areaWhere['user_id'] = session('user_id');
        $areaWhere['is_default'] = "1";
        $area_id = M('address')->where($areaWhere)->getField('city_id');
        $trans = M('transport_extend')->where('transport_id =' . $transport_id)->select();
        foreach ($trans as $key => $value) {
            $arr = explode(',', (trim($value['area_id'], ',')));
            $p = array_search($area_id, $arr);
            if ($p !== FALSE) {
                //如果传过来的ID存在与特殊区域，后去特殊区域的邮费
                if($value['xprice']>0 && $goodsNum>1){
                    $yunfei = $value['sprice'] +  $value['xprice']*ceil(($goodsNum-1/$value['xnum']));
                }else{
                    $yunfei = $value['sprice'];
                }
            }
        }

        if($yunfei == NULL ){
            $trans = M('transport_extend')->where('is_default =1 and transport_id ='.$transport_id)->find();
            if($trans['xprice']>0 && $goodsNum>1){
                $yunfei = $trans['sprice'] +  $trans['xprice']*ceil(($goodsNum-1/$trans['xnum']));
            }else{
                $yunfei = $trans['sprice'];
            }
        }
        //判断此商品是否满足免邮费的条件，满足邮费为0
        $where['id'] = $transport_id;
        $transport = M('transport')->where($where)->find();
        if($transport['money'] !=0){
            if ($transport['money'] <= ($goodsPrice)) {
                $yunfei = 0;
            }
        }
        if($transport['num'] != 0){
            if ($goodsNum >= $transport['num']) {
                $yunfei = 0;
            }
        }
        
    } else {
        $yunfei = 0;
    }
    return $yunfei;
}

//获取手机端运费的信息
function getTransportM($transport_id) {
    if ($transport_id != 0) {
        //获取商品的运费 
        $trans = M('transport_extend')->where('transport_id =' . $transport_id)->select();
        foreach ($trans as $key => $value) {
            $arr = explode(',', (trim($value['area_id'], ',')));
            $p = array_search(240, $arr);
            if ($p !== FALSE) {
                //如果传过来的ID存在与特殊区域，后去特殊区域的邮费
                $yunfei = $value['sprice'];
            }else{
                $yunfei = $trans = M('transport_extend')->where('is_default =1 and transport_id ='.$transport_id)->getField('sprice');
            }
        }
        //判断此商品是否满足免邮费的条件，满足邮费为0
        $where['id'] = $transport_id;
        $transport = M('transport')->where($where)->find();
        if ($transport['money'] != 0 ) {
            $transports = "<span class=\"tag-red tag\">店铺满".$transport['money']."元包邮</span>";
        }else if($transport['num'] != 0){
            $transports = "<span class=\"tag-red tag\">店铺满".$transport['num']."件包邮</span>";
        }else{
            if($yunfei > 0){
                $transports = $yunfei."元";
            }else{
                $transports = "<span class=\"tag-red tag\">免运费</span>";
            }
        }
    } else {
        $transports = "<span class=\"tag-red tag\">免运费</span>";
    }
    return $transports;
}



//获取商品店铺的购物券 
function getCoupon($store_id) {
    $where['voucher_store_id'] = $store_id;
    $where['voucher_start_date'] = array('ELT', NOW_TIME);
    $where['voucher_end_date'] = array('EGT', NOW_TIME);
    $where['voucher_state'] = 1;
    $re = M('pVoucherRule')->where($where)->order('voucher_price desc')->find();
    if ($re == NULL) {
        return "";
    } else {
        $txt = "<a  class='Man_Coupon_main_font_a3 position' href=" . U('Coupon/detail/' . auth_encode($store_id)) . "><b>领券</b><span>满" . $re['voucher_limit'] . "减" . $re['voucher_price'] . "</span></a>";
        return $txt;
    }
}

//获取商品店铺的购物券 
function getDiscount($store_id) {
    $where['voucher_store_id'] = $store_id;
    $where['voucher_start_date'] = array('ELT', NOW_TIME);
    $where['voucher_end_date'] = array('EGT', NOW_TIME);
    $where['voucher_state'] = 1;
    $re = M('pVoucherRule')->where($where)->order('voucher_price desc')->find();
    if ($re == NULL) {
        return "";
    } else {
        $txt = "<a  class='Man_Coupon_main_font_a3 position' href=" . U('Coupon/detail/' . auth_encode($store_id)) . "><b>限购</b><span>50&nbsp;件</span></a>";
        return $txt;
    }
}

/**
 * 根据商品的数量的总价获取店铺的优惠状况
 * @author ubten
 * @param $goods_id $int 商品的ID
 * @param $goods_num, $int 商品的数量
 * @param $coupon_id, $int 优惠券的ID
 * @param $totalPrice, $int 订单的总价（适合购物车支付的时候）
 * @param $type, $int 0为手机获取  1为PC获取
 * @return 返回优惠信息 及 优惠后的总价
 */
function getStoreCoupon($goods_id, $goods_num, $coupon_id = 0, $totalPrice = 0, $shop_id = 0, $type = 0) {
    if ($totalPrice == 0) {
        $map['id'] = $goods_id;
        $getPrice = M('wxdgoods')->field('group_id,shop_price')->where($map)->find();
        $a['totalPrice'] = $getPrice['shop_price'] * $goods_num; //计算出总价
    } else {
        $a['totalPrice'] = $totalPrice;
        $getPrice['group_id'] = $shop_id;
    }

    //获取商家的优惠信息
    $pMan = D('PMansong');
    $where['store_id'] = $getPrice['group_id'];
    $where['start_date'] = array('ELT', NOW_TIME);
    $where['end_date'] = array('EGT', NOW_TIME);
    $where['state'] = 1;
    $mansong = $pMan->relation('pMansong')->where($where)->select();

    //计算出用户适合的优惠信息
    if (empty($mansong)) {
        //为空则没有优惠活动
        $a['mansong'] = "";
    } else {
        //不为空则返回最适合的优惠活动
        //遍历去除不适合的优惠活动
        foreach ($mansong as $key => $value) {
            $voupon = $value['pMansong'];
            foreach ($voupon as $k => $v) {
                if ($v['price']*100 > $totaolPrice*100) {
                    unset($v[$k]);
                }else{
                    $coupon[$key] = $v;
                }
            }
        }
        //判断是否为空，并排序，根据优惠价格提取最高的优惠
        if (empty($voupon)) {
            //为空则没有达到优惠活动金额，返回空
            $a['mansong'] = "";
        } else {
            //合并数组并排序（按从大到小）
            $b = 0;
            foreach ($coupon as $key => $value) {
                //p($value);
                    $arr[$b] = $v;
                    $b++;
            }
            //排序并取出最高的优惠活动金额
            $coupon = array_sort($arr, 'discount', 1, 'desc');
            $a['mansong'] = "<input type=\"hidden\" name=\"mansong_id\" value=\"" . $coupon['rule_id'] . "\">商家促销：订单满" . $coupon['price'] . "元 减" . $coupon['discount'];
            $a['totalPrice'] = $a['totalPrice'] - $coupon['discount'];
        }


        //获取个人的优惠券
        //获取商家的优惠信息
        $pVoucher = D('PVoucher');
        $where['voucher_owner_id'] = session('user_id');
        $where['voucher_store_id'] = $getPrice['group_id'];
        $where['voucher_start_date'] = array('ELT', NOW_TIME);
        $where['voucher_end_date'] = array('EGT', NOW_TIME);
        $where['voucher_state'] = 1;
        $voucher = $pVoucher->where($where)->select();

        if ($type == 1) {
            foreach ($voucher as $value) {
                if ($a['totalPrice'] > $value['voucher_limit']) {
                    if ($coupon_id == $value['voucher_id']) {
                        $vouchers .="<li><input type=\"radio\" name=\"voucherid\" value=\"" . $value['voucher_id'] . "\"  checked=\"true\" class=\"f_checkbox_t\"><label class=\"fl shipping_title\" for=\"\">优惠：" . $value['voucher_price'] . "元 " . $value['voucher_title'] . "</label></li>";
                    } else {
                        $vouchers .="<li><input type=\"radio\" name=\"voucherid\" value=\"" . $value['voucher_id'] . "\" class=\"f_checkbox_t\"><label class=\"fl shipping_title\" for=\"\">优惠：" . $value['voucher_price'] . "元 " . $value['voucher_title'] . "</label></li>";
                    }
                } else {
                    $vouchers = "";
                }
            }
        } else {
            $vouchers .="<select onchange=\"goods_cp_dp($(this))\" class=\"goods_cp_dp\" name=\"1111111111\">";
            $vouchers .="<option value=\"0\" cp_dp_id=\"0\" selected=\"selected\">店铺优惠券</option>";
            foreach ($voucher as $value) {
                if ($a['totalPrice'] > $value['voucher_limit']) {
                    if ($coupon_id == $value['voucher_id']) {
                        $vouchers .="<option selected=\"selected\" value=\"" . $value['voucher_id'] . "\" cp_dp_id=\"" . $value['voucher_id'] . "\">优惠：" . $value['voucher_price'] . "元 " . $value['voucher_title'] . "</option>";
                    } else {
                        $vouchers .="<option value=\"" . $value['voucher_id'] . "\" cp_dp_id=\"" . $value['voucher_id'] . "\">优惠：" . $value['voucher_price'] . "元 " . $value['voucher_title'] . "</option>";
                    }
                } else {
                    $vouchers = "";
                }
            }
            $vouchers .=" </select>";
        }
        $a['voucher'] = $vouchers;
    }
    return $a;
}

//手机端获取平台的优惠券(淘嘟嘟商城发布的优惠券信息)
function getTddVoucher($totalPrice) {
    $map['voucher_owner_id'] = session('user_id');
    $map['voucher_state'] = 1;
    $map['voucher_store_id'] = 0;
    $map['voucher_start_date'] = array('ELT', NOW_TIME);
    $map['voucher_end_date'] = array('EGT', NOW_TIME);
    $arr = M('pVoucher')->where($map)->select();

    if ($arr) {
        $html = "<section class=\"order_info zong_order_info\" style=\"margin-top: 8px;\">";
        $html .= "<div class=\"order_list\">";
        $html .= "<div class=\"checkout_other\">";
        $html .= "<div class=\"fy \" href=\"javascript:void(0);\" onclick=\"showCheckoutOther2(this);\">平台优惠券";
        $html .= "<span class=\"right_arrow_flow\" style=\" float:right; margin-right:30px;\"></span>";
        $html .= "<span class=\"Freight\" id=\"Freight_pt\" class=\"backnone\" style=\" float:right; margin-right:10px;\">有优惠券可用</span>";
        $html .= "</div>";
        $html .= "<div class=\"subbox_other subbox_other_show\">";
        $html .= "<div id=\"tddyhq\" class=\"flow_bottom_list flow_bottom_list_show\">";
        $html .= "<div onclick=\"hidetittle($(this))\" class=\"flow_bottom_list_tittle\">淘嘟嘟优惠券</div>";
        $html .= "<ul class=\"pt_coupon\">";

        foreach ($arr as $key => $value) {
//            p($value['voucher_limit']);
//            p($totalPrice);
            if ($totalPrice >= $value['voucher_limit']){
                $html .= "<li onclick=\"goods_cp_dp($(this))\" class=\"\">";
                $html .= "<label class=\"fl shipping_title\" for=\"\"><input type=\"radio\" name=\"tddvoucherid\"  m_price=\"" . $value['voucher_price'] . "\"  value=\"" . $value['voucher_id'] . "\" class=\"f_checkbox_t Freight_Checked\">";
                $html .= "<label class=\"fl shipping_title\" for=\"\">" . $value['voucher_title'] . "：满" . $value['voucher_limit'] . "减" . $value['voucher_price'] . "</label>";
                $html .= "</li>";
            }
        }

        $html .= "</ul></div></div></div></div></section>";
    }
    return $html;
}

//PC端获取店铺优惠券
function getStoreVoucher_Mobile($totalPrice, $store_id) {
    $map['voucher_owner_id'] = session('user_id');
    $map['voucher_state'] = 1;
    $map['voucher_store_id'] = $store_id;
    $map['voucher_end_date'] = array('EGT', NOW_TIME);
    $arr = M('pVoucher')->where($map)->select();
    if ($arr) {
        foreach ($arr as $key => $value) {
            if ($value['voucher_limit'] <= $totalPrice) {
                $vouchers .="<li onclick=\"goods_cp_dp($(this))\"><input type=\"radio\"  m_price=\"" . $value['voucher_price'] . "\" name=\"YHQstore[" . $store_id . "]\" value=\"" . $value['voucher_id'] . "\" class=\"f_checkbox_t\"><label class=\"fl shipping_title\" for=\"\">优惠：" . $value['voucher_price'] . "元 " . $value['voucher_title'] . "</label></li>";
            }
        }
    }
    return $vouchers;
}

//PC端获取店铺优惠券
function getStoreVoucher_PC($totalPrice, $store_id) {
    $map['voucher_owner_id'] = session('user_id');
    $map['voucher_state'] = 1;
    $map['voucher_store_id'] = $store_id;
    $map['voucher_end_date'] = array('EGT', NOW_TIME);
    $arr = M('pVoucher')->where($map)->select();
    if ($arr) {
        $html = "<select onchange=\"goods_cp_dp($(this))\"  class=\"pt_cp1\" name=\"YHQstore[" . $store_id . "]\">";
        $a .= "<option goods_postage=\"0\" cp_dp_id=\"0\" selected=\"selected\" value=\"0\">您有可使用的优惠券</option>";
        foreach ($arr as $key => $value) {
            if ($value['voucher_limit'] <= $totalPrice) {
                $b .= "<option goods_postage=\"" . $value['voucher_price'] . "\" cp_dp_id=\"" . $value['voucher_price'] . "\" value=\"" . $value['voucher_id'] . "\">本店铺：满" . $value['voucher_limit'] . "减" . $value['voucher_price'] . "</option>";
            }
        }
        $c .= "</select>";
    }
    if (!empty($b)) {
        return $html . $a . $b . $c;
    } else {
        return $b;
    }
}

//PC端获取平台优惠券
function getTddVoucher_PC($totalPrice) {
    $map['voucher_owner_id'] = session('user_id');
    $map['voucher_state'] = 1;
    $map['voucher_store_id'] = 0;
    $map['voucher_end_date'] = array('EGT', NOW_TIME);

    $arr = M('pVoucher')->where($map)->select();
    if ($arr) {
        $html = "<select class=\"pt_cp1\" onchange=\"goods_cp_dp($(this))\"  name=\"tddvoucherid\">";
        $a .= "<option goods_postage=\"0\" cp_dp_id=\"0\" selected=\"selected\" value=\"0\">您有可使用的优惠券</option>";
        foreach ($arr as $key => $value) {
            if ($value['voucher_limit'] <= $totalPrice) {
                $b .= "<option goods_postage=\"" . $value['voucher_price'] . "\" m_price=\"" . $value['voucher_price'] . "\" value=\"" . $value['voucher_id'] . "\">" . $value['voucher_title'] . "：满" . $value['voucher_limit'] . "减" . $value['voucher_price'] . "</option>";
            }
        }
        $c .= "</select>";
    }

    if (!empty($b)) {
        return $html . $a . $b . $c;
    } else {
        return $b;
    }
}

//获取平台的满减价格(淘嘟嘟商城发布的优惠券信息)
//返回满减后的优惠券
//$store_id  如果store_id为0则表示平台的优惠券，否则为商家的
//
//
function tddMansong($totaolPrice, $store_id = 0) {
    //获取平台的满减送
    $pMan = D('PMansong');
    $where['store_id'] = $store_id;
    $where['start_time'] = array('ELT', NOW_TIME);
    $where['end_time'] = array('EGT', NOW_TIME);
    $where['state'] = 2;
    $mansong = $pMan->relation('pMansong')->where($where)->select();
    //计算出用户适合的优惠信息
    if (empty($mansong)) {
        //为空则没有优惠活动
        $a['mansong'] = "";
        $a['totalPrice'] = $totaolPrice;
    } else {
        //不为空则返回最适合的优惠活动
        //遍历去除不适合的优惠活动
        foreach ($mansong as $key => $value) {
            $voupon = $value['pMansong'];
            foreach ($voupon as $k => $v) {
                if ($v['price']*100 > $totaolPrice*100) {
                    unset($v[$k]);
                }else{
                    $coupon[$key] = $v;
                }
            }
        }
        //判断是否为空，并排序，根据优惠价格提取最高的优惠
        if (empty($coupon)) {
            //为空则没有达到优惠活动金额，返回空
            $a['mansong'] = "";
            $a['totalPrice'] = $totaolPrice;
        } else {
            //合并数组并排序（按从大到小）
            $b = 0;
            foreach ($coupon as $key => $value) {
                    $arr[$b] = $value;
                    $b++;
            }
            //排序并取出最高的优惠活动金额
            $coupon = array_sort($arr, 'discount', 1, 'desc');
            $a['mansong'] = "<input class=\"m_price\" type=\"hidden\" name=\"MSstore[" . $store_id . "]\" m_price=\"" . price($coupon['discount']) . "\" value=\"" . $coupon['rule_id'] . "\">优惠活动：订单满" . price($coupon['price']) . "元 减" . price($coupon['discount']);
            $a['totalPrice'] = $totaolPrice - $coupon['discount'];
        }
    }
    return $a;
}

//获取商家店铺的优惠额度
function getStoreVoucher($order_id, $voucherid, $mansongid) {
    //商家的优惠券额度
    if (!empty($voucherid)) {
        $map1['voucher_id'] = $voucherid;
        $map1['voucher_owner_id'] = session('user_id');
        $map1['voucher_state'] = 1;
        $map1['voucher_end_date'] = array('EGT', NOW_TIME);
        $voucher = M('pVoucher')->where($map1)->find();
        //如果获取到值将此优惠信息保存到数据表中
        if ($voucher) {
            $data1['pay_sn'] = $order_id;
            $data1['voucher_store'] =1 ;
            $data1['voucher_type'] = 1;  //优惠类型 1为店铺优惠 2为平台优惠 3 满减送优惠
            $data1['voucher_code'] = $voucherid;  //优惠编号 个优惠的ID
            $data1['voucher_price'] = $voucher['voucher_price'];
            $data1['voucher_desc'] = $voucher['voucher_title'];
            //增加记录
            M('orderVoucher')->add($data1);
            //标记优惠券状态
            M('pVoucher')->where('voucher_id =' . $voucherid)->setField('voucher_state', 2);
        }
        $voucher_price = $voucher['voucher_price'];
    } else {
        $voucher_price = 0;
    }

    //店铺的订单满减额度
    if (!empty($mansongid)) {
        $map3['rule_id'] = $mansongid;
        $mansong = M('pMansongRule')->where($map3)->find();
        //如果获取到值将此优惠信息保存到数据表中
        if ($mansong) {
            $data3['pay_sn'] = $order_id;
            $data3['voucher_store'] = 1;
            $data3['voucher_type'] = 3;  //优惠类型 1为店铺优惠 2为平台优惠 3 满减送优惠
            $data3['voucher_code'] = $mansongid;  //优惠编号 个优惠的ID
            $data3['voucher_price'] = $mansong['discount'];
            $data3['voucher_desc'] = "店铺满：" . $mansong['price'] . "减" . $mansong['discount'];
            M('orderVoucher')->add($data3);
        }
        $mansong_price = $mansong['discount'];
    } else {
        $mansong_price = 0;
    }

    return price($voucher_price + $mansong_price);
}

//获取平台的优惠额度
function getTaoduduVoucher($order_id, $tddvoucherid,$mansongid) {
    //平台优惠券ID
    if (!empty($tddvoucherid)) {
        $map2['voucher_id'] = $tddvoucherid;
        $map2['voucher_owner_id'] = session('user_id');
        $map2['voucher_state'] = 1;
        $map2['voucher_end_date'] = array('EGT', NOW_TIME);
        $tddvoucher = M('pVoucher')->where($map2)->find();
        //如果获取到值将此优惠信息保存到数据表中
        if ($tddvoucher) {
            $data2['pay_sn'] = $order_id;
            $data2['voucher_store'] = 0;
            $data2['voucher_type'] = 2;  //优惠类型 1为店铺优惠 2为平台优惠 3 满减送优惠
            $data2['voucher_code'] = $tddvoucherid;  //优惠编号 个优惠的ID
            $data2['voucher_price'] = $tddvoucher['voucher_price'];
            $data2['voucher_desc'] = $tddvoucher['voucher_title'];
            M('orderVoucher')->add($data2);
            M('pVoucher')->where('voucher_id =' . $tddvoucherid)->setField('voucher_state', 2);
        }
        $tddvoucher_price = $tddvoucher['voucher_price'];
    } else {
        $tddvoucher_price = 0;
    }

    //平台订单满减额度
    if (!empty($mansongid)) {
        $map3['rule_id'] = $mansongid;
        $mansong = M('pMansongRule')->where($map3)->find();
        //如果获取到值将此优惠信息保存到数据表中
        if ($mansong) {
            $data3['pay_sn'] = $order_id;
            $data3['voucher_store'] = 0;
            $data3['voucher_type'] = 3;  //优惠类型 1为店铺优惠 2为平台优惠 3 满减送优惠
            $data3['voucher_code'] = $mansongid;  //优惠编号 个优惠的ID
            $data3['voucher_price'] = $mansong['discount'];
            $data3['voucher_desc'] = "店铺满：" . $mansong['price'] . "减" . $mansong['discount'];
            M('orderVoucher')->add($data3);
        }
        $mansong_price = $mansong['discount'];
    } else {
        $mansong_price = 0;
    }
    return price($tddvoucher_price + $mansong_price);
}

/**
 * 用户订单使用优惠信息记录（并将用户优惠券标记为使用中）
 * @param $order_id      用户订单ID
 * @param $voucherid     商家优惠券ID
 * @param $tddvoucherid  平台优惠券ID
 * @param $mansongid     满减送ID
 * @return 返回总共优惠的金额
 *  */
function UserOrderVoucherRecord($order_id, $voucherid, $tddvoucherid, $mansongid) {

    if (!empty($voucherid)) {
        $map1['voucher_id'] = $voucherid;
        $map1['voucher_owner_id'] = session('user_id');
        $map1['voucher_state'] = 1;
        $map1['voucher_end_date'] = array('EGT', NOW_TIME);
        $voucher = M('pVoucher')->where($map1)->find();
        //如果获取到值将此优惠信息保存到数据表中
        if ($voucher) {
            $data1['order_sn'] = $order_id;
            $data1['voucher_type'] = 1;  //优惠类型 1为店铺优惠 2为平台优惠 3 满减送优惠
            $data1['voucher_code'] = $voucherid;  //优惠编号 个优惠的ID
            $data1['voucher_price'] = $voucher['voucher_price'];
            $data1['voucher_desc'] = $voucher['voucher_title'];
            //增加记录
            M('orderVoucher')->add($data1);
            //标记优惠券状态
            M('pVoucher')->where('voucher_id =' . $voucherid)->setField('voucher_state', 2);
        }
        $voucher_price = $voucher['voucher_price'];
    } else {
        $voucher_price = 0;
    }

    if (!empty($tddvoucherid)) {
        $map2['voucher_id'] = $tddvoucherid;
        $map2['voucher_owner_id'] = session('user_id');
        $map2['voucher_state'] = 1;
        $map2['voucher_end_date'] = array('EGT', NOW_TIME);
        $tddvoucher = M('pVoucher')->where($map2)->find();
        //如果获取到值将此优惠信息保存到数据表中
        if ($tddvoucher) {
            $data2['order_sn'] = $order_id;
            $data2['voucher_type'] = 2;  //优惠类型 1为店铺优惠 2为平台优惠 3 满减送优惠
            $data2['voucher_code'] = $tddvoucherid;  //优惠编号 个优惠的ID
            $data2['voucher_price'] = $tddvoucher['voucher_price'];
            $data2['voucher_desc'] = $tddvoucher['voucher_title'];
            M('orderVoucher')->add($data2);
            M('pVoucher')->where('voucher_id =' . $tddvoucherid)->setField('voucher_state', 2);
        }
        $tddvoucher_price = $tddvoucher['voucher_price'];
    } else {
        $tddvoucher_price = 0;
    }

    if (!empty($mansongid)) {
        $map3['rule_id'] = $mansongid;
        $mansong = M('pMansongRule')->where($map3)->find();
        //如果获取到值将此优惠信息保存到数据表中
        if ($mansong) {
            $data3['order_sn'] = $order_id;
            $data3['voucher_type'] = 3;  //优惠类型 1为店铺优惠 2为平台优惠 3 满减送优惠
            $data3['voucher_code'] = $mansongid;  //优惠编号 个优惠的ID
            $data3['voucher_price'] = $mansong['discount'];
            $data3['voucher_desc'] = "店铺满：" . $mansong['price'] . "减" . $mansong['discount'];
            M('orderVoucher')->add($data3);
        }
        $mansong_price = $mansong['discount'];
    } else {
        $mansong_price = 0;
    }
    //p($order_id);
    //p($voucherid);
    //p($tddvoucherid);
    //p($mansongid);
    //p($voucher_price);
    //p($tddvoucher_price);
    //p($mansong_price);
    //exit;
    $total = price($voucher_price + $tddvoucher_price + $mansong_price);
    return $total;
}
